package com.totem.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.totem.admin.mapper.TAppConfigItemMapper;
import com.totem.admin.model.TAppConfigItem;
import com.totem.admin.service.ITAppConfigItemService;
import com.totem.base.enums.AppConfigItemNameEnum;
import com.totem.base.enums.SubscribeNameEnum;
import com.totem.base.enums.SwitchEnum;
import com.totem.startup.InitialGlobalConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@Service
public class TAppConfigItemServiceImpl extends ServiceImpl<TAppConfigItemMapper, TAppConfigItem> implements ITAppConfigItemService {

    @Override
    public void initData() {
        List<TAppConfigItem> all = super.list();
        Map<String, TAppConfigItem> itemMap = all.stream().collect(Collectors.toMap(TAppConfigItem::getItemName, Function.identity()));
        TAppConfigItem subscribeShowSpecialItem = itemMap.get(SubscribeNameEnum.SUBSCRIBE_SHOW_SPECIAL.getCode());
        String subscribeShowSpecialStr = subscribeShowSpecialItem.getItemValue();
        Boolean subscribeShowSpecial = subscribeShowSpecialStr.equalsIgnoreCase(SwitchEnum.ON.getCode());
        TAppConfigItem fundReturnRateItem = itemMap.get(AppConfigItemNameEnum.FUND_RETURN_RATE.getCode());
        BigDecimal fundReturnRate = new BigDecimal(fundReturnRateItem.getItemValue());
        TAppConfigItem fundWithdrawFeeItem = itemMap.get(AppConfigItemNameEnum.FUND_WITHDRAW_FEE.getCode());
        BigDecimal fundWithdrawFee = new BigDecimal(fundWithdrawFeeItem.getItemValue());
        TAppConfigItem withdrawMinItem = itemMap.get(AppConfigItemNameEnum.WITHDRAW_MIN.getCode());
        String withdrawMin = withdrawMinItem.getItemValue();
        TAppConfigItem withdrawMaxItem = itemMap.get(AppConfigItemNameEnum.WITHDRAW_MAX.getCode());
        String withdrawMax = withdrawMaxItem.getItemValue();
        TAppConfigItem withdrawFeeItem = itemMap.get(AppConfigItemNameEnum.WITHDRAW_FEE.getCode());
        BigDecimal withdrawFee = new BigDecimal(withdrawFeeItem.getItemValue());
        TAppConfigItem usdtCollectionAddrItem = itemMap.get(AppConfigItemNameEnum.USDT_COLLECTION_ADDR.getCode());
        String usdtCollectionAddr = usdtCollectionAddrItem.getItemValue();
        TAppConfigItem chargePrivateKeyItem = itemMap.get(AppConfigItemNameEnum.CHARGE_PRIVATE_KEY.getCode());
        String chargePrivateKey = chargePrivateKeyItem.getItemValue();
        TAppConfigItem chargeAddressItem = itemMap.get(AppConfigItemNameEnum.CHARGE_ADDRESS.getCode());
        String chargeAddress = chargeAddressItem.getItemValue();
        TAppConfigItem balanceConversionFeeItem = itemMap.get(AppConfigItemNameEnum.BALANCE_CONVERSION_FEE.getCode());
        BigDecimal balanceConversionFee = new BigDecimal(balanceConversionFeeItem.getItemValue());

        InitialGlobalConfig initialGlobalConfig = InitialGlobalConfig.instance();
        initialGlobalConfig.init(subscribeShowSpecial, fundReturnRate, fundWithdrawFee, withdrawMin, withdrawMax, withdrawFee, usdtCollectionAddr, chargePrivateKey, chargeAddress, balanceConversionFee);
    }
}
